<template>
  <!--  shadow radius -->
  <div class="flex-column container">
    <el-table
      class="two-table"
      border
      :data="data.PermisstionArr"
      style="max-height: 100%; width: 100%"
      row-key="id"
    >
      <!-- <el-table-column label="#" width="80" type="index" :index="1 + data.pageSize * (data.currentPage - 1)"
        align="center"></el-table-column> -->
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code"></el-table-column>
      <el-table-column label="修改时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作" width="320" align="center">
        <template #default="scope">
          <el-button
            size="small"
            :disabled="scope.row.level === 4 ? true : false"
            @click="addPermission(scope.row)"
          >
            {{ scope.row.level === 3 ? "添加功能" : "添加菜单" }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            :disabled="scope.row.level === 1 ? true : false"
            @click="updatePermission(scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            :title="`你确定要删除 ${scope.row.name}?`"
            width="260px"
            @confirm="removeMenu(scope.row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                :disabled="scope.row.level === 1 ? true : false"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框组件：添加或者更新已有的菜单的数据结构 -->
    <el-dialog
      v-model="dialogVisible"
      :title="menuData.id ? '更新菜单' : '添加菜单'"
    >
      <el-form>
        <el-form-item label="名称">
          <el-input
            placeholder="请你输入菜单的名称"
            v-model="menuData.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-input
            placeholder="请你输入权限数值"
            v-model="menuData.code"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import {
  reqAllPermission,
  reqAddOrUpdateMenu,
  reqRemoveMenu,
} from "../../../api/acl/menu/index";
import type {
  Permission,
  PermissionResponseData,
  MenuParams,
} from "../../../api/acl/menu/type";
// 获取骨架的小仓库
// import useLayoutSettingsStore from "../../../store/modules/settings";

// let settingStore = useLayoutSettingsStore();

// const background = ref(true)
// const size = ref('default')
const dialogVisible = ref(false);

const data = reactive({
  currentPage: 1,
  pageSize: 20,
  total: 0,
  PermisstionArr: [],
});

let menuData = reactive<MenuParams>({
  code: "",
  level: 0,
  name: "",
  pid: 0,
});

// 页面加载
onMounted(() => {
  getPermission();
});

// 获取数据列表
const getPermission = async () => {
  let res: PermissionResponseData = await reqAllPermission();
  if (res.code === 200) {
    data.PermisstionArr = res.data as [];
  }
};

// 添加菜单
const addPermission = (row: Permission) => {
  // 重置数据
  Object.assign(menuData, {
    id: 0,
    code: "",
    level: 0,
    name: "",
    pid: 0,
  });
  // 打开弹框
  dialogVisible.value = true;
  // 赋值
  menuData.level = row.level + 1;
  menuData.pid = row.id as number;
};
// 编辑已有的菜单
const updatePermission = (row: Permission) => {
  dialogVisible.value = true;
  // 将表格行的数据带入画面，赋值给对象menuData
  Object.assign(menuData, row);
};
// 弹框 添加或更新 菜单的保存
const save = async () => {
  dialogVisible.value = false;
  // 发请求：新增子菜单|更新某一个己有的菜单的数据
  let res: any = await reqAddOrUpdateMenu(menuData);
  if (res.code === 200) {
    dialogVisible.value = false;
    ElMessage({
      type: "success",
      message: menuData.id ? "更新成功" : "添加成功",
    });
    getPermission();
  }
};

// 删除菜单
const removeMenu = async (id: any) => {
  let res = await reqRemoveMenu(id);
  if (res.code === 200) {
    ElMessage({ type: "success", message: "删除成功" });
    getPermission();
  }
};
</script>

<style scoped lang="scss">
@import "@/styles/common.scss";

.header {
  padding: 10px;
  box-sizing: border-box;
  height: 60px;
  width: 100%;
  margin-bottom: 10px;

  .form {
    margin-top: 5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

.content {
  flex: 1;
  box-sizing: border-box;
  overflow: hidden;
  padding: 12px;

  .addHeader {
    height: 40px;
    width: 100%;
    margin-bottom: 5px;
  }
}
</style>
